package com.bowlong.third.grizzly;

import java.io.IOException;
import java.util.Map;

import org.glassfish.grizzly.Buffer;

import com.bowlong.bio3.B3InputStream;
import com.bowlong.bio3.B3OutputStream;
import com.bowlong.io.ByteOutStream;
import com.bowlong.objpool.ByteOutPool;
import com.bowlong.util.NewMap;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class GrizzlyEx {
	public static NewMap readNewMap(Buffer buffer) throws IOException {
		Map map = readMap(buffer);
		return new NewMap(map);
	}

	public static Map readMap(Buffer buffer) throws IOException {
		GrizzlyInputStream gis = new GrizzlyInputStream(buffer);
		return B3InputStream.readMap(gis);
	}

	public static byte[] writeMap(Map map) throws IOException {
		ByteOutStream out = ByteOutPool.borrowObject();
		try {
			B3OutputStream.writeMap(out, map);
			return out.toByteArray();
		} finally {
			ByteOutPool.borrowObject();
		}
	}
}
